package com.situ.onlinebookstore.front.controller.api;

import com.situ.onlinebookstore.front.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/")
public class PasswordApiController {
    private final UserService userService;
    @PostMapping("/rePassword")
    public void rePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1-获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String rePassword = request.getParameter("rePassword");
        try {
            //2-校验参数 //3-16
            if (password == null || !password.matches("^.{3,16}$")) {
                throw new Exception("密码格式不正确");
            }
            if (rePassword == null || !rePassword.matches("^.{3,16}$")) {
                throw new Exception("新密码格式不正确");
            }
            if (!rePassword.equals(password)) {
                throw new Exception("两次密码不一致");
            }
//            3-调用service
            userService.rePassword(username, password, rePassword);
            //返回JSON
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append("{\"code\": 0}");

        } catch (Exception e) {
            e.printStackTrace();
            //修改失败
            //返回JSON    失败， 错误信息
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append("{\"code\": 1, \"msg\": \"" + e.getMessage() + "\"}");
        }
    }

    @PostMapping("/confirmPwd")
    public void confirmPwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1-获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String confirmPwd = request.getParameter("confirmPwd");
        if (username == null || password == null || confirmPwd == null) {
            // 返回错误信息或跳转到错误页面
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append("{\"code\": 1, \"msg\": \"参数不完整\"}");
            return;
        }

        try {
            //2-校验参数 //3-16
            if (!password.matches("^.{3,16}$")) {
                throw new Exception("密码格式不正确");
            }
            if (!confirmPwd.matches("^.{3,16}$")) {
                throw new Exception("新密码格式不正确");
            }
            if (!confirmPwd.equals(password)) {
                throw new Exception("两次密码不一致");
            }


//            3-调用service
            userService.confirmPwd(username, password);
            //返回JSON
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append("{\"code\": 0}");

        } catch (Exception e) {
            e.printStackTrace();
            //修改失败
            //返回JSON    失败， 错误信息
            response.setContentType("application/json; charset=UTF-8");
            response.getWriter().append("{\"code\": 1, \"msg\": \"" + e.getMessage() + "\"}");
        }
    }
}
